import { useEffect, useRef } from "react";
import { useState } from "react";

export function useCountDown(count = 10) {

    const [num, setNum] = useState(count)
    const [isDisabled, setIsDisabled] = useState(false)
    const timer = useRef(null)
    const fn = () => {
        if (timer.current !== null) return
        setIsDisabled(true)
        timer.current = setInterval(() => {
            setNum((c) => c - 1)
        }, 1000)
        console.log(Math.round(Math.random() * 100))
    }
    useEffect(() => {
        if (num <= 0) {
            clearInterval(timer.current)
            setNum(count)
            setIsDisabled(false)
            timer.current = null
        }
    }, [num, count])
    return [fn, num, isDisabled]
}